package com.ocom.oauth.auth.xcx;

import com.alibaba.fastjson.JSON;
import com.ocom.common.entity.master.EntityWechatAppInfo;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.RequestUtil;
import com.ocom.oauth.client.HumanClientService;
import com.ocom.oauth.client.PayConfigClientService;
import com.ocom.security.authentication.YoCiUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 *
 *  说明：自定义的获取用户信息
 */
@Service
@Slf4j
public class XcxUserDetailService {


    @Autowired
    HumanClientService humanClientService;

//    @Value("${xcx.appid}")
//    String appid;
//
//    @Value("${xcx.secret}")
//    String secret;

    @Autowired
    PayConfigClientService payConfigClientService;

    //绑定到单位的默认值
//    public  static Map<String , AppInfo> app = new HashMap(){
//        {
//            put("39", new AppInfo("wxcb170317c5a70550","3427c300b77c8a1fc216d4af4f39a992"));
//            put("1", new AppInfo("wxe13d55590479da25","68c531eb3507142161f22a53eb9c1c06"));
//        }
//    };

    //获取用户
    public UserDetails loadByCode(String code,String comId) {
        //默认
        String appid = "wxe13d55590479da25";
        String secret = "ba161cda71ad2f3bda5dbdd6b2c0efca";
        Result result  = payConfigClientService.getWxInfo(Long.valueOf(comId),0l,"xcx");
        if(result.getCode()==200){
            EntityWechatAppInfo entityWechatAppInfo = JSON.parseObject(JSON.toJSONString(result.getData()),EntityWechatAppInfo.class);
            appid=entityWechatAppInfo.getAppid();
            secret=entityWechatAppInfo.getAppsecret();
        }
        String url="https://api.weixin.qq.com/sns/jscode2session?appid="+ appid +"&secret="+ secret +"&js_code="+  code +"&grant_type=authorization_code";
        log.info("url:" + url);
//        if(app.containsKey(comId)){
//            AppInfo appInfo = app.get(comId);
//            url="https://api.weixin.qq.com/sns/jscode2session?appid="+ appInfo.getAppId() +"&secret="+ appInfo.getAppSecret() +"&js_code="+  code +"&grant_type=authorization_code";
//        }

        RequestUtil requestUtil=new RequestUtil();
        Map<String,String> map =requestUtil.doGetRequest(url);
        log.info("map:" + map);
        if(map.containsKey("errcode")){
            throw new InvalidGrantException("无效的code");
        }

        //{errcode=40013, errmsg=invalid appid, rid: 61ceb753-441d1ecc-356d9165}
        //{openid=oYviH4npaE9N38FfW-bZg4bcXijY, session_key=GzzTarsUi+gkRNxDT+AfPg==}

        List<GrantedAuthority> authorities = new ArrayList<>();
        authorities.add(new SimpleGrantedAuthority("ROLE_XCX"));

        Set<String> permissions = new HashSet<>();
        permissions.add("sys:user:admin");

        return new YoCiUser(1L, Long.valueOf(comId),0L ,"0", permissions,map.get("openid"), appid, true, true, true, true, authorities,new ArrayList<>());
    }

    //
}
